﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Controls;
using System.Collections;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace nl.tno.ict.multitouch.wpfmultitouch.controls
{
    public class ButtonAdorner : Adorner
    {        
        #region Data

        private ArrayList _logicalChildren;
        private Point _point;
        private MTButton _buttonShowKeyBoard;
        private MTImage _imageShowKeyBoard;

        #endregion // Data

        #region Constructor        
        
        public ButtonAdorner(MTTextbox adornedElement)
            : base(adornedElement)
        {
            //this._point = point;
            
            _buttonShowKeyBoard = new MTButton();
            _buttonShowKeyBoard.Width = 32;
            _buttonShowKeyBoard.Height = 32;
            _buttonShowKeyBoard.Background = new SolidColorBrush(Colors.Transparent);
            _buttonShowKeyBoard.BorderThickness = new Thickness(0);
            _buttonShowKeyBoard.PreviewMouseDown += new System.Windows.Input.MouseButtonEventHandler(_button_PreviewMouseDown);
            _buttonShowKeyBoard.PreviewMouseMove += new System.Windows.Input.MouseEventHandler(_button_PreviewMouseMove);
            _buttonShowKeyBoard.PreviewMouseUp += new System.Windows.Input.MouseButtonEventHandler(_button_PreviewMouseUp);
            _buttonShowKeyBoard.BorderBrush = new SolidColorBrush(Colors.Transparent);

            StackPanel panel = new StackPanel();
            panel.Orientation = Orientation.Horizontal;
            panel.Margin = new Thickness(0);

            _imageShowKeyBoard = new MTImage();
            BitmapImage bi3 = new BitmapImage(new Uri(@"Adorner\Icons\ShowKeyboard.gif", UriKind.Absolute));
            _imageShowKeyBoard.Stretch = Stretch.Fill;
            _imageShowKeyBoard.Source = bi3;
            _imageShowKeyBoard.Margin = new Thickness(0);
            panel.Children.Add(_imageShowKeyBoard);

            _buttonShowKeyBoard.Content = panel;

            _buttonShowKeyBoard.Click += new RoutedEventHandler(_button_Click);
            
            // Register the Button with the element tree so that
            // it will be rendered, and can inherit DP values.
            base.AddLogicalChild(_buttonShowKeyBoard);
            base.AddVisualChild(_buttonShowKeyBoard);
        }

        #endregion // Constructor

        private void _button_PreviewMouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (((FrameworkElement)AdornedElement).Parent is MTCanvas)
                ((MTCanvas)((FrameworkElement)AdornedElement).Parent).MTCanvas_PreviewMouseLeftButtonUp(sender, e);
            else
                ((MTGrid)((FrameworkElement)AdornedElement).Parent).MTGrid_PreviewMouseLeftButtonUp(sender, e);
        }

        private void _button_PreviewMouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (((FrameworkElement)AdornedElement).Parent is MTCanvas)
                ((MTCanvas)((FrameworkElement)AdornedElement).Parent).MTCanvas_PreviewMouseMove(sender, e);
            else
                ((MTGrid)((FrameworkElement)AdornedElement).Parent).MTGrid_PreviewMouseMove(sender, e);
        }

        private void _button_PreviewMouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (((FrameworkElement)AdornedElement).Parent is MTCanvas)
                ((MTCanvas)((FrameworkElement)AdornedElement).Parent).MTCanvas_PreviewMouseLeftButtonDown(sender, e);            
            else
                ((MTGrid)((FrameworkElement)AdornedElement).Parent).MTGrid_PreviewMouseLeftButtonDown(sender, e);            
        }

        private void _button_Click(object sender, RoutedEventArgs e)
        {            
            if (!((MTCanvas)((MTTextbox)AdornedElement).Parent).Children.Contains(((MTTextbox)AdornedElement).Keyboard))
            {
                ((MTCanvas)((MTTextbox)AdornedElement).Parent).Children.Add(((MTTextbox)AdornedElement).Keyboard);
                if (MTCanvas.GetBottom(AdornedElement) < ((MTTextbox)AdornedElement).Keyboard.Height + 5)
                    MTCanvas.SetTop(((MTTextbox)AdornedElement).Keyboard, MTCanvas.GetTop(AdornedElement) - ((MTTextbox)AdornedElement).Keyboard.Height - 5);
                else
                    MTCanvas.SetTop(((MTTextbox)AdornedElement).Keyboard, MTCanvas.GetTop((MTTextbox)AdornedElement) + ((MTTextbox)AdornedElement).Height + 5);

                MTCanvas.SetLeft(((MTTextbox)AdornedElement).Keyboard, MTCanvas.GetLeft((MTTextbox)AdornedElement) - ((Math.Abs(((MTTextbox)AdornedElement).Keyboard.Width - ((MTTextbox)AdornedElement).Width)) / 2));
            }
        }        
        
        #region Measure/Arrange

        /// <summary>
        /// Allows the Button to determine how big it wants to be.
        /// </summary>
        /// <param name="constraint">A limiting size for the Button.</param>
        protected override Size MeasureOverride(Size constraint)
        {
            _buttonShowKeyBoard.Measure(constraint);
            return _buttonShowKeyBoard.DesiredSize;
        }

        /// <summary>
        /// Positions and sizes the Button.
        /// </summary>
        /// <param name="finalSize">The actual size of the TextBlock.</param>		
        protected override Size ArrangeOverride(Size finalSize)
        {
            Point location = _point;//new Point(0, 0);
            Rect rect = new Rect(location, finalSize);
            _buttonShowKeyBoard.Arrange(rect);
            return finalSize;
        }

        #endregion // Measure/Arrange

        #region Visual Children

        /// <summary>
        /// Required for the Button to be rendered.
        /// </summary>
        protected override int VisualChildrenCount
        {
            get { return 1; }
        }

        /// <summary>
        /// Required for the Button to be rendered.
        /// </summary>
        protected override Visual GetVisualChild(int index)
        {
            if (index != 0)
                throw new ArgumentOutOfRangeException("index");

            return _buttonShowKeyBoard;
        }

        #endregion // Visual Children

        #region Logical Children

        /// <summary>
        /// Required for the Button to inherit property values
        /// from the logical tree, such as FontSize.
        /// </summary>
        protected override IEnumerator LogicalChildren
        {
            get
            {
                if (_logicalChildren == null)
                {
                    _logicalChildren = new ArrayList();
                    _logicalChildren.Add(_buttonShowKeyBoard);
                }

                return _logicalChildren.GetEnumerator();
            }
        }

        #endregion // Logical Children
    }
}
